<?php
/**
 * 售后服务控制器
 * @author ezhu <ezhu@jipukeji.com>
 * @version 1.0
 */

namespace Shop\Controller;

class ServiceController extends CommonController{
    
    private $refund_type = array(
            'item' => '购物订单',
            'redpacket' => '红包'
    );
    
    private $payment_type = array(
            'alipay' => '支付宝',
            'alipaywap' => '手机支付宝',
            'bankpay' => '网银支付',
            'wechatpay' => '微信支付',
            'crowdfunding' => '众筹支付'
    );
    
    
    /**
     * 退款申请记录
     */
    public function refund(){
        $where = array();
        $where['type'] = 1;
        $where['store_id'] = $this->store_id;
        // 获取退款列表
        $list = $this->lists('Refund', $where, 'id desc', null);
        $map = array(
                'seller_status' => array(
                        1 => '<span class="text-danger">待审核</span>',
                        2 => '<span class="text-success">已处理</span>',
                        3 => '<span class="text-danger">不同意</span>'
                ),
                'refund_status' => array(
                        1 => '<span class="text-danger">待商家处理</span>',
                        2 => '<span class="text-danger">待管理员处理</span>',
                        3 => '<span class="text-success">已退款</span>'
                ),
                'refund_type' => $this->refund_type,
                'payment_type' => $this->payment_type
        );
        int_to_string($list, $map);
        //记录当前列表页的Cookie
        Cookie('__forward__',$_SERVER['REQUEST_URI']);
        
        //模板输出变量赋值
        $this->assign('_list', $list);
        $this->meta_title = '退款列表';
        $this->display();
    }
    
    
    
    /**
     * 商户审核订单
     */
    public function audit(){
        $id = I('id');
        $status = I('status');
        if(empty($id)|| empty($status) || !in_array($status,array(2,3))){
            $this->error('参数错误');
        }
        $model = M('Refund');
        $map['id'] = $id;
        $map['store_id'] = $this->store_id;
        $info = $model->where($map)->find();
        if(!$info){
            $this->error('退款订单不存在');
        }
        if($info['seller_status'] > 1 || $info['refund_status'] > 1){
            $this->error('等待管理员操作');
        }
        $model->startTrans();
        $rst = $model->where($map)->save(array('seller_status'=>$status));
        if($rst){
            if($status == 2){ //同意
                $res = $model->where($map)->save(array('refund_status'=>2));
                if($res){
                    $model->commit();
                    $this->success('已同意退款，等待管理员处理');
                }else{
                    $model->rollback();
                    $this->error('操作失败！');
                }
                exit;
            }else{ // 驳回
                $oMap['id'] = $info['order_id'];
                $res = M('Order')->where($oMap)->save(array('o_status'=>405));
                if($res){
                    $model->commit();
                    $this->success('退款已拒绝');
                }else{
                    $model->rollback();
                    $this->error('拒绝操作失败！');
                }
                exit;
            }
        }else{
            $model->rollback();
        }
        
    }
    
    
    
}